package com.k.rocketmq.demo;

import com.k.rocketmq.RocketMqConst;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;

public class MyProducer {
    public static void main(String[] args) {
        send();
    }

    public static void send(){
        System.out.println("生产者 start");
        String producerGroup=RocketMqConst.PRODUCER_GROUP;
        String namesrvAddr=RocketMqConst.NAME_SRV_ADDR_89;
        String topic=RocketMqConst.COMMAND_TEST_89_TOPIC;
        String tag=RocketMqConst.COMMAND_TEST_89_TOPIC_TAG;
        try {
            //生产者的组名
            DefaultMQProducer producer  = new DefaultMQProducer(RocketMqConst.PRODUCER_GROUP);
            //指定NameServer地址，多个地址以 ; 隔开
            //如 producer.setNamesrvAddr("192.168.100.141:9876;192.168.100.142:9876;192.168.100.149:9876");
            producer.setNamesrvAddr(namesrvAddr);
            producer.setVipChannelEnabled(false);
            producer.setProducerGroup(producerGroup);
            // Producer对象在使用之前必须要调用start初始化，只能初始化一次
            producer.start();
            for (int i = 0; i < 1; i++) {
                //4.创建消息对象，指定主题Topic、Tag和消息体
                /**
                 * 参数一：消息主题Topic
                 * 参数二：消息Tag
                 * 参数三：消息内容
                 */
//                Message msg = new Message("first-topic", null, ("Hello World" + i).getBytes());
                String sendJson = "{\"data\":{\"address\":\"testzg自动化1007\",\"createTerminalIdentify\":\"\",\"createTerminalType\":1,\"createTime\":\"2024-08-07 16:31:24\",\"createUserId\":\"\",\"createUserName\":\"\",\"createUserOrgCode\":\"\",\"createUserOrgIdentifier\":\"\",\"createUserOrgName\":\"\",\"id\":\"a548dcc344034c6ab4eb37112f82df44\",\"idCard\":\"231801195810121214\",\"latitude\":\"29.371793\",\"longitude\":\"104.89645\",\"modifyTerminalIdentify\":\"\",\"modifyTerminalType\":1,\"modifyTime\":\"2024-08-08 16:31:24\",\"modifyUserId\":\"\",\"modifyUserName\":\"\",\"modifyUserOrgCode\":\"\",\"modifyUserOrgName\":\"\",\"name\":\"钱冯冯冯卫100001\",\"objectNumber\":\"unKnow\",\"objectType\":\"5\",\"sourceType\":\"YBDX\",\"valid\":1,\"warningModelCode\":\"fd15dcf1-345d-4cce-a4e5-067a3047069f\",\"warningTime\":\"2024-08-09 16:31:24\",\"warningType\":\"often\"},\"sendTime\":\"2024-08-10 16:31:24\"}";
                String idCard = "11111019400101536"+i;
                sendJson="{\"data\":{\"address\":\"testzg智慧小区0001\",\"createTerminalIdentify\":\"\",\"createTerminalType\":1,\"createTime\":1723401313080,\"createTimeStr\":\"2024-08-12 02:35:13\",\"createUserId\":\"\",\"createUserName\":\"\",\"createUserOrgCode\":\"\",\"createUserOrgIdentifier\":\"\",\"createUserOrgName\":\"\",\"id\":\"4b1761f9d1fd4b44b3810f242193508d\",\"idCard\":\""+idCard+"\",\"latitude\":\"0.0\",\"longitude\":\"0.0\",\"modifyTerminalIdentify\":\"\",\"modifyTerminalType\":1,\"modifyTime\":1723401313080,\"modifyTimeStr\":\"2024-08-12 02:35:13\",\"modifyUserId\":\"\",\"modifyUserName\":\"\",\"modifyUserOrgCode\":\"\",\"modifyUserOrgName\":\"\",\"name\":\"钱冯冯冯冯100000\",\"objectNumber\":\"460205360100000\",\"objectType\":\"phone\",\"sourceType\":\"YBDX\",\"valid\":1,\"warningModelCode\":\"4c879ef6-c87a-4a63-a786-ce13d42a028d\",\"warningTime\":\"2024-08-12T10:35:13\",\"warningTimeStr\":\"2024-08-12 10:35:13\",\"warningType\":\"often\"},\"sendTime\":1723430113578}";
                sendJson="{\"data\":{\"address\":\"testzg智慧小区0001\",\"attachment\":\"\",\"businessId\":\"\",\"createTerminalIdentify\":\"\",\"createTerminalType\":1,\"createTime\":1723489334924,\"createTimeStr\":\"2024-08-13 03:02:14\",\"createUserId\":\"\",\"createUserName\":\"\",\"createUserOrgCode\":\"\",\"createUserOrgIdentifier\":\"\",\"createUserOrgName\":\"\",\"earlyWarningReason\":\"\",\"id\":\"02177ced94414224ab7abe0337e31de0\",\"idCard\":\""+idCard+"\",\"latitude\":\"0.0\",\"longitude\":\"0.0\",\"modifyTerminalIdentify\":\"\",\"modifyTerminalType\":1,\"modifyTime\":1723489334924,\"modifyTimeStr\":\"2024-08-13 03:02:14\",\"modifyUserId\":\"\",\"modifyUserName\":\"\",\"modifyUserOrgCode\":\"\",\"modifyUserOrgName\":\"\",\"name\":\"钱冯冯冯冯100000\",\"objectNumber\":\"460205360100000\",\"objectType\":\"phone\",\"sourceType\":\"YBDX\",\"subtype\":\"\",\"valid\":1,\"warningLevelCode\":\"\",\"warningModelCode\":\"4c879ef6-c87a-4a63-a786-ce13d42a028d\",\"warningTime\":\"2024-08-13T11:02:14\",\"warningTimeStr\":\"2024-08-13 11:02:14\",\"warningType\":\"often\"},\"sendTime\":1723518135337}";
                Message msg = new Message(topic,tag, sendJson.getBytes());
                //5.发送消息
                SendResult result = producer.send(msg);
                //发送状态
                SendStatus status = result.getSendStatus();
                System.out.println("发送结果:" + result+":"+status);

//                Message msg2 = new Message("first-topic", "510#role1,role3", ("tag Hello World" + i).getBytes());
//                SendResult result2 = producer.send(msg2);
                //发送状态
//                SendStatus status2 = result.getSendStatus();
//                System.out.println("发送结果2:" + result2+":"+status2);

                //线程睡1秒
                TimeUnit.SECONDS.sleep(1);
            }
            //6.关闭生产者producer 一般在应用上下文，关闭的时候进行关闭，用上下文监听器
            producer.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("生产者 finished");
    }
}
